//
//  WDCircleView.m
//  画圆环动画
//
//  Created by Mac on 16/12/11.
//  Copyright © 2016年 wangdi. All rights reserved.
//

#import "WDCircleView.h"
#define WDColor(s,a) [UIColor colorWithRed:(((s & 0xFF0000) >> 16))/255.0 green:(((s & 0xFF00) >>8)/255.0) blue:((s &0xFF)/255.0) alpha:a]
#define pi 3.14159265359
#define WDCircle(degrees) ((pi * degrees)/ 180)

@interface WDCircleView ()
@property(strong,nonatomic)UIBezierPath *path;
/** oneLayer */
@property(strong,nonatomic)CAShapeLayer *shapeLayer;
/** anotherLayer*/
@property(strong,nonatomic)CAShapeLayer *anotherShapeLayer;
/** 动画1 */
@property (nonatomic, strong) CABasicAnimation *shapeAnimation;
/** 动画2 */
@property (nonatomic, strong) CABasicAnimation *anotherShapeAnimation;
/** 背景图层 */
@property(strong,nonatomic)CAShapeLayer *bgLayer;
@end

@implementation WDCircleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //旋转90°，让起点定在上面
        CGAffineTransform transform = CGAffineTransformIdentity;
        self.transform = CGAffineTransformRotate(transform, -M_PI / 2);
        
        _path = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
        
        //创建一个背景圆环
        _bgLayer = [CAShapeLayer layer];
        _bgLayer.frame = self.bounds;
        _bgLayer.fillColor = [UIColor clearColor].CGColor;
        _bgLayer.lineWidth = 2.f;
        _bgLayer.strokeColor = WDColor(0x000000, 0.1).CGColor;
        _bgLayer.strokeStart = 0.f;
        _bgLayer.strokeEnd = 1.f;
        
        _bgLayer.path = _path.CGPath;
        [self.layer addSublayer:_bgLayer];
        
        //创建圆环1
        _shapeLayer = [CAShapeLayer layer];
        _shapeLayer.frame = self.bounds;
        _shapeLayer.fillColor = [UIColor clearColor].CGColor;
        _shapeLayer.lineWidth = 2.f;
        _shapeLayer.lineCap = kCALineCapButt;
        _shapeLayer.strokeColor = [UIColor redColor].CGColor;
        _shapeLayer.strokeStart = 0.f;
        _shapeLayer.strokeEnd = 0.f;
        
        _shapeLayer.path = _path.CGPath;
        [self.layer addSublayer:_shapeLayer];
        
        //创建圆环2
        _anotherShapeLayer = [CAShapeLayer layer];
        _anotherShapeLayer.frame = self.bounds;
        _anotherShapeLayer.fillColor = [UIColor clearColor].CGColor;
        _anotherShapeLayer.lineWidth = 2.f;
        _anotherShapeLayer.lineCap = kCALineCapButt;
        _anotherShapeLayer.strokeColor = [UIColor redColor].CGColor;
        _anotherShapeLayer.strokeStart = 0.f;
        _anotherShapeLayer.strokeEnd = 0.f;
        
        _anotherShapeLayer.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.origin.x + self.bounds.size.width / 2, self.bounds.origin.y + self.bounds.size.height / 2) radius:(self.bounds.size.width / 2) startAngle:WDCircle(0) endAngle:WDCircle(360) clockwise:NO].CGPath;
        
        [self.layer addSublayer:_anotherShapeLayer];
        
    }
    return self;
}

- (void)setStartValue:(CGFloat)startValue
{
    _startValue = startValue;
    _shapeLayer.strokeStart = startValue;
    _anotherShapeLayer.strokeStart = startValue;
}

- (void)setValue:(CGFloat)value{
    
    _value = value;
    _shapeLayer.strokeEnd = value;
    _anotherShapeLayer.strokeEnd = 1.f - value;
    
    if (!self.isRemark) {
        
        //添加动画1
        _shapeAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        _shapeAnimation.duration = 1;
        _shapeAnimation.fromValue = [NSNumber numberWithFloat:self.startValue];
        _shapeAnimation.toValue = [NSNumber numberWithFloat:value];
        [_shapeLayer addAnimation:_shapeAnimation forKey:nil];
        _shapeAnimation.removedOnCompletion = NO;
        _shapeAnimation.fillMode = kCAFillModeForwards;
        
        //添加动画2
        _anotherShapeAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        _anotherShapeAnimation.duration = 1;
        _anotherShapeAnimation.fromValue = [NSNumber numberWithFloat:self.startValue];
        _anotherShapeAnimation.toValue = [NSNumber numberWithFloat:1.f - value];
        [_anotherShapeLayer addAnimation:_anotherShapeAnimation forKey:nil];
        _anotherShapeAnimation.removedOnCompletion = NO;
        _anotherShapeAnimation.fillMode = kCAFillModeForwards;
        
    }
}

-(void)setLineColr:(UIColor *)lineColr{
    _lineColr = lineColr;
    _shapeLayer.strokeColor = lineColr.CGColor;
}

- (void)setAnotherLineColor:(UIColor *)anotherLineColor
{
    _anotherLineColor = anotherLineColor;
    _anotherShapeLayer.strokeColor = anotherLineColor.CGColor;
}

-(void)setLineWidth:(CGFloat)lineWidth{
    _lineWidth = lineWidth;
    _shapeLayer.lineWidth = lineWidth;
    _anotherShapeLayer.lineWidth = lineWidth;
    _bgLayer.lineWidth = lineWidth;
}

@end
